package com.technology.education.ui.common.withdraw

import android.os.Bundle
import android.view.View
import androidx.core.os.bundleOf
import com.technology.education.R
import com.technology.education.data.SpKeys
import com.technology.education.databinding.FragmentWithdrawBinding
import com.technology.education.ext.*
import com.technology.education.ui.common.information.CompleteWithdrawActivity
import me.hgj.jetpackmvvm.base.fragment.BaseVmDbFragment
import me.hgj.jetpackmvvm.ext.parseState
import me.hgj.jetpackmvvm.util.startActivity

/**
 * 提现页面
 * type 0 为教师申请退回保证金,保证金金额固定不能修改
 * 1 教师积分提现
 * 2 学生积分提现-学生提现使用单独的页面 {@see com.technology.education.ui.student.withdraw.WithdrawStudentFragment}
 * 3 推广人员余额提现
 * 4 教师时长提现
 * 5.学生积分兑换时长
 */
class WithdrawFragment : BaseVmDbFragment<WithdrawViewModel, FragmentWithdrawBinding>() {
    override fun initView(savedInstanceState: Bundle?) {
        mViewModel.type = arguments?.getInt("type", 0) ?: 0
        mViewModel.money.set(arguments?.getString("money") ?: "0")
        mDatabind.toolBar.toolbar.setNavigationOnClickListener {
            back()
        }
        mViewModel.withdrawAccount.set(getAppViewModel().userInfo.value?.alipayNo?:"")
        mDatabind.toolBar.tvTitle.text = when (mViewModel.type) {
            0 -> "退回保障金"
            1, 2 -> {
                mViewModel.buttonText.set("立即提现")
                "积分提现"
            }
            3 -> {
                mViewModel.buttonText.set("立即提现")
                "余额提现"
            }
            4 -> {
                mViewModel.titleText.set("时长提现")
                mViewModel.buttonText.set("立即提现")
                "时长提现"
            }
            5 -> {
                mDatabind.tv2.text = "全部兑换 "
                mDatabind.group.visibility = View.GONE
                mViewModel.buttonText.set("立即兑换")
                "兑换时长"
            }
            else -> "提现"
        }
        mDatabind.vm = mViewModel
        mDatabind.tvModify.setOnClickListener {
            startActivity(CompleteWithdrawActivity::class, Pair("type", 1))
        }
        if (mViewModel.type > 0) {
            mViewModel.getData()
        }
    }

    override fun createObserver() {
        observe(SpKeys.MODIFY_WITHDRAW_ACCOUNT, String::class.java) {
            mViewModel.withdrawAccount.set(it)
        }
        mViewModel.applyResult.observe(this) {
            parseState(it, onSuccess = { it ->
                getAppViewModel().getInfo(true)
                if (mViewModel.type == 5) {
                    toast("兑换的时长已添加时长余额")
                    back()
                } else {
                    toast("我们将在七个工作日内处理您的申请")
                    navigation(
                        requireView(), R.id.action_withdrawFragment_to_withDrawDetailFragment,
                        bundleOf("id" to it.id, "type" to mViewModel.type)
                    )
                }
            }, onError = { err -> toast(err.errorMsg) })
        }

    }

    override fun showLoading(message: String) {

    }

    override fun dismissLoading() {

    }
}